package com.zhiyou100.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhiyou100.aspect.MyLogAspect;
import com.zhiyou100.model.MLog;
import com.zhiyou100.model.MUser;
import com.zhiyou100.model.PublishTheProject;
import com.zhiyou100.service.AdminService;
import com.zhiyou100.util.MyLogAnno;
import com.zhiyou100.util.ResponseObject;

@Controller
@RequestMapping("/admin")
public class AdminController {

	@Autowired
	AdminService service;
	
	@MyLogAnno("管理员进入首页")
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index() {
		// 跳转到首页
		System.out.println("进入跳转界面");
		return "/admin/index";
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String adminLogin1() {
		// 跳转到登陆页面
		System.out.println("进入跳转界面");
		return "/admin/admin_login";
	}
	
	@MyLogAnno("管理员登录")
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String adminLogin2(String u_name,String u_password,HttpServletRequest request) {
		// 接收判断...
		System.out.println("从界面接收到的后台登录用户是 : "+u_name+","+u_password);
		Map<String,Object> map = new HashMap<>();
		request.getSession().setAttribute("mName",u_name);
		map.put("u_name",u_name);
		map.put("u_password",u_password);
		MUser mUser = service.findAdminNameAndAnminPassword(map);
		System.out.println("查到的管理员是 : "+mUser);
		if(mUser != null) {
			// 跳转到登陆页面
			return "/admin/index";
		}
		return "/admin/admin_login";
	}

	// 所有项目
	@MyLogAnno("管理员查看所有项目")
	@RequestMapping(value = "/allProducts", method = RequestMethod.GET)
	public String allProject(Model model,@RequestParam(defaultValue="")String pname,
			@RequestParam(defaultValue="")String pson,
			@RequestParam(defaultValue="")String pstate,
			@RequestParam(defaultValue="")String ptype) {
		System.out.println("查看所有项目中接收到的关键字是 : "+"项目类型 --  :"+pname+" -- 项目发起人 : "+pson+" -- 项目状态 : "+pstate+" -- 项目类型 :"+ptype);
		Map<String,Object> map = new HashMap<>();
		map.put("pname", pname);
		map.put("pson", pson);
		map.put("pstate", pstate);
		map.put("ptype", ptype);
		
		List<PublishTheProject> lists = service.findAll(map);
		System.out.println("查出的全部项目为 : "+lists);
		model.addAttribute("lists",lists);
		System.out.println("model + "+model);
		return "/admin/products";
	}

	// 项目详情
	@MyLogAnno("管理员查看项目详情")
	@RequestMapping(value = "/product-info1",method=RequestMethod.GET)
	public String lookProject(Model model,int id) {
		System.out.println("接收到的id 是 : "+id);
		PublishTheProject ptp = service.findPtpById(id);
		System.out.println("查出的项目详情是 : "+ptp);
		model.addAttribute("ptp",ptp);
		return "/admin/product/product-info1";
	}
	
	// 项目审核
	@MyLogAnno("管理员查看项目审核")
	@RequestMapping(value = "/allAudit", method = RequestMethod.GET)
	public String allAudit(Model model,@RequestParam(defaultValue="")String pname,
	@RequestParam(defaultValue="")String pson,
	@RequestParam(defaultValue="")String ptype) {
		Map<String,Object> map = new HashMap();
		map.put("pname", pname);
		map.put("pson", pson);
		map.put("ptype", ptype);
		List<PublishTheProject> lists = service.allAudit(map);
		
		System.out.println("查出的全部审核项目为 : "+lists);
		model.addAttribute("lists",lists);
		System.out.println("model + "+model);
		return "/admin/audit";
	}
	
	// 项目审核详情
	@MyLogAnno("管理员进入某个审核项目的详情")
	@RequestMapping(value = "/audit-info2",method=RequestMethod.GET)
	public String lookAudit(Model model,int id) {
		System.out.println("接收到的id 是 : "+id);
		PublishTheProject ptp = service.findPtpById(id);
		System.out.println("查出的项目详情是 : "+ptp);
		model.addAttribute("ptp",id);
		model.addAttribute("ptp",ptp);
		return "/admin/product/audit-info2";
	}
	
	// 项目审核状态更新
	@RequestMapping(value = "/update", method= RequestMethod.POST)
	@ResponseBody
	@MyLogAnno("管理员进行了审核操作")
	public ResponseObject updteAudit(@RequestBody PublishTheProject publishTheProject) {
		System.out.println("更新状态接收到的信息是 : "+publishTheProject);
		int row = service.update(publishTheProject);
		System.out.println("更新的行数为 : "+row);
		if(row > 0) {
			return new ResponseObject("200","成功",publishTheProject);         
		}
		return new ResponseObject("404","失败",publishTheProject);
	}
 	
	// 日志管理
		@MyLogAnno("管理员查看日志")
		@RequestMapping(value = "/lookLog", method = RequestMethod.GET)
		public String lookLog(Model model,@RequestParam(defaultValue="")String MName) {
			Map<String,Object> map = new HashMap();
			map.put("MName",MName);
			List<MLog> lists = service.lookLog(map);
			System.out.println("查出的全部日志为 : "+lists);
			model.addAttribute("lists",lists);
			return "/admin/adminLog";
		}
	
	// 问题管理
		@MyLogAnno("管理员进入首页")
		@RequestMapping(value = "/help", method = RequestMethod.GET)
		public String help() {
			// 跳转到问题界面
			System.out.println("进入跳转问题界面");
			return "/admin/help";
		}
		
	// 后台管理人员 - 全部
		@RequestMapping(value = "/allMUser",method = RequestMethod.GET)
		@MyLogAnno("管理员查看所有管理员")
		public String allMUser(Model model) {
			List<MUser> lists = service.findAllMUser();
			System.out.println("查出的全部项目为 : "+lists);
			model.addAttribute("lists",lists);
			return "/admin/muser/muser";
		}
		
	// 后台管理人员-添加
		@MyLogAnno("管理员进入添加界面")
		@RequestMapping(value = "addMUserUI", method = RequestMethod.GET)
		public String addMUserUI() {
			return "/admin/muser/addmuser";
		}
		
		// 后台管理人员-添加
		@MyLogAnno("管理员开始添加")
		@RequestMapping(value = "addMUser", method = RequestMethod.GET)
		public String addMUser(MUser muser) {
			System.out.println("后台管理人员添加" +muser);
			int row = service.addMUser(muser);
			return "forward:/admin/allMUser";
		}
		
		// 后台管理人员 - 详情/更改
		@MyLogAnno("管理员开始更新信息")
		@ResponseBody
		@RequestMapping(value = "/updata", method = RequestMethod.PUT)
		public ResponseObject updata(@RequestBody MUser mUser) {
			System.out.println("后台管理人员修好的信息是" +mUser);
			int row = service.updata(mUser);
			if(row > 0) {
				return new ResponseObject("200","成功",mUser);
			}
			return null;
			
		}
		
		// 后台管理人员 - 删除	
		@MyLogAnno("管理员开始删除")
		@RequestMapping(value = "delete", method = RequestMethod.GET)
		public String delete(int id) {
			System.out.println("删除的id : " +id);
			service.delete(id);
			return "forward:/admin/allMUser";
		}
		
		// 管理员退出
		@MyLogAnno("管理员退出")
		@RequestMapping(value = "out", method = RequestMethod.GET)
		public String outAdmin(HttpServletRequest request) {
			System.out.println("管理员要退出了...");
			request.getSession().invalidate();
			
			
			return "forward:/login.jsp";
			
		}
		
	// 项目分类
	@RequestMapping(value = "/productClass", method = RequestMethod.GET)
	public String productClass() {
		// ..查数据库...

		return "/admin/classify/all_classify";
	}
}
